package com.pig4cloud.pig.module.controller.wx;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.DateUtils;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.wx.UserRegCashRecord;
import com.pig4cloud.pig.module.service.wx.IUserRegCashRecordService;
import com.pig4cloud.pig.module.service.wx.IWxUserService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 用户分享后得现金奖励的记录Controller
 *
 * @author ruoyi
 * @date 2022-06-10
 */
@Validated
@Api(value = "用户分享后得现金奖励的记录控制器", tags = {"用户分享后得现金奖励的记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/user/reg/cash/record")
public class UserRegCashRecordController {

    private final IUserRegCashRecordService userRegCashRecordService;
	private final IWxUserService wxUserService;

    /**
     * 查询用户分享后得现金奖励的记录列表
     */
    @ApiOperation("查询用户分享后得现金奖励的记录列表")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<UserRegCashRecord> list(@Validated UserRegCashRecord userRegCashRecord) {
        QueryWrapper<UserRegCashRecord> qw = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        if(userRegCashRecord.getRecordType() !=null){
        	map.put("recordType",userRegCashRecord.getRecordType() );
		}
        if(StrUtil.isNotEmpty(userRegCashRecord.getNickName())){
			map.put("nickName",userRegCashRecord.getNickName());
		}
        if(StrUtil.isNotEmpty(userRegCashRecord.getBeginDate())){
			map.put("beginTime",userRegCashRecord.getBeginDate() + " 00:00:00");
			map.put("endTime",userRegCashRecord.getEndDate()  + " 23:59:59");
		}

        TableDataInfo<UserRegCashRecord> td = PageUtils.buildDataInfo(userRegCashRecordService.selectRecordList(PageUtils.buildPage(),map));
        List<UserRegCashRecord> list = td.getData();
        if(CollUtil.isNotEmpty(list)){
        	for (UserRegCashRecord cashRecord : list){
        		if(StrUtil.isNotEmpty(cashRecord.getNickName())){
					cashRecord.setNickName(EmojiParser.parseToUnicode(cashRecord.getNickName()));
				}
				if(StrUtil.isNotEmpty(cashRecord.getRegNickName())){
					cashRecord.setRegNickName(EmojiParser.parseToUnicode(cashRecord.getRegNickName()));
				}
			}
		}
        Double cashMoney = userRegCashRecordService.selectCashMoney(map);
        Map<String,Object> mapOther = new HashMap<>();
		mapOther.put("cashMoney",cashMoney);
		td.setOtherData(mapOther);

        return td;
    }

    /**
     * 导出用户分享后得现金奖励的记录列表
     */
    @ApiOperation("导出用户分享后得现金奖励的记录列表")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:export')")
    @SysLog("用户分享后得现金奖励的记录")
    @GetMapping("/export")
    public List export(@Validated UserRegCashRecord userRegCashRecord) {
//		QueryWrapper<UserRegCashRecord> qw = new QueryWrapper<>();
//		qw.eq("pay_state",1);
//		if(StrUtil.isNotEmpty(userRegCashRecord.getBeginDate())){
//			qw.between("create_time",userRegCashRecord.getBeginDate() + " 00:00:00",userRegCashRecord.getEndDate() + " 23:59:59");
//		}

		Map<String,Object> map = new HashMap<>();
		if(userRegCashRecord.getRecordType() !=null){
			map.put("recordType",userRegCashRecord.getRecordType() );
		}
		if(StrUtil.isNotEmpty(userRegCashRecord.getNickName())){
			map.put("nickName",userRegCashRecord.getNickName());
		}
		if(StrUtil.isNotEmpty(userRegCashRecord.getBeginDate())){
			map.put("beginTime",userRegCashRecord.getBeginDate() + " 00:00:00");
			map.put("endTime",userRegCashRecord.getEndDate()  + " 23:59:59");
		}

        List<UserRegCashRecord> list = userRegCashRecordService.selectRecordListExport(map);
		if(CollUtil.isNotEmpty(list)){
			for (UserRegCashRecord cashRecord : list){
//				WxUser wxUser = wxUserService.findById(cashRecord.getUserId());
//				if(wxUser!=null){
//					cashRecord.setNickName(EmojiParser.parseToUnicode(wxUser.getNickName()));
//				}
//				WxUser wxUserReg = wxUserService.findById(cashRecord.getRegUserId());
//				if(wxUserReg!=null){
//					cashRecord.setRegNickName(EmojiParser.parseToUnicode(wxUserReg.getNickName()));
//				}

				if(StrUtil.isNotEmpty(cashRecord.getNickName())){
					cashRecord.setNickName(EmojiParser.parseToUnicode(cashRecord.getNickName()));
				}
				if(StrUtil.isNotEmpty(cashRecord.getRegNickName())){
					cashRecord.setRegNickName(EmojiParser.parseToUnicode(cashRecord.getRegNickName()));
				}

				cashRecord.setCreateTimeStr(DateUtils.getTime(cashRecord.getCreateTime()));
				cashRecord.setPayStateStr("打款成功");
			}
		}
        // ExcelUtil<UserRegCashRecord> util = new // ExcelUtil<>(UserRegCashRecord.class);
        return  list;// util.exportExcel(list, "用户分享后得现金奖励的记录");
    }

    /**
     * 获取用户分享后得现金奖励的记录详细信息
     */
    @ApiOperation("获取用户分享后得现金奖励的记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:query')")
    @GetMapping("/{id}")
    public R<UserRegCashRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        UserRegCashRecord userRegCashRecord = userRegCashRecordService.getById(id);
        return R.ok(userRegCashRecord);
    }

    /**
     * 新增用户分享后得现金奖励的记录
     */
    @ApiOperation("新增用户分享后得现金奖励的记录")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:add')")
    @SysLog("用户分享后得现金奖励的记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody UserRegCashRecord userRegCashRecord) {
        return R.okByRow(userRegCashRecordService.save(userRegCashRecord) ? 1 : 0);
    }

    /**
     * 修改用户分享后得现金奖励的记录
     */
    @ApiOperation("修改用户分享后得现金奖励的记录")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:edit')")
    @SysLog("用户分享后得现金奖励的记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody UserRegCashRecord userRegCashRecord) {
        return R.okByRow(userRegCashRecordService.updateById(userRegCashRecord) ? 1 : 0);
    }

    /**
     * 删除用户分享后得现金奖励的记录
     */
    @ApiOperation("删除用户分享后得现金奖励的记录")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:remove')")
    @SysLog("用户分享后得现金奖励的记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(userRegCashRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 获取用户分享后得现金奖励的记录详细信息
     */
    @ApiOperation("获取用户分享后得现金奖励的记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:userRegCashRecord:query')")
    @GetMapping("new")
    public List newUser(@Validated UserRegCashRecord userRegCashRecord) {
        QueryWrapper<UserRegCashRecord> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(userRegCashRecord.getBeginDate())){
            qw.between("create_time",userRegCashRecord.getBeginDate() + " 00:00:00",userRegCashRecord.getEndDate() + " 23:59:59");
        }
        TableDataInfo<UserRegCashRecord> td = PageUtils.buildDataInfo(userRegCashRecordService.page(PageUtils.buildPage(),qw));
        List<UserRegCashRecord> list = td.getData();
        Integer num=0;
        Double money=0.00;
        for(UserRegCashRecord list1:list){
            money+=list1.getCashMoney().doubleValue();
            if (list1.getRecordType()==0){
                if (list1.getFromType()==1){
                    num+=1;
                }
            }
        }
        double one = (double)Math.round(money*100)/100;
        List list2 = new ArrayList<>();
        list2.add(num);
        list2.add(one);
        return list2;
    }
}
